package com.thunbu.thunbu_meeting_system.activity

import androidx.recyclerview.widget.LinearLayoutManager
import com.cathu.lib_commonkt.base.BaseMVVMActivity
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.animator.EmptyAnimator
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupPosition
import com.lxj.xpopup.interfaces.SimpleCallback
import com.thunbu.lib_common.widget.CommonToolBar
import com.thunbu.thunbu_meeting_system.BaseVBQuickAdapter
import com.thunbu.thunbu_meeting_system.R
import com.thunbu.thunbu_meeting_system.bean.AssetModel
import com.thunbu.thunbu_meeting_system.bean.AssetTypeModel
import com.thunbu.thunbu_meeting_system.bindAdapter
import com.thunbu.thunbu_meeting_system.databinding.ActivityMyAssetsBinding
import com.thunbu.thunbu_meeting_system.databinding.ItemInventoryListItemOtherBinding
import com.thunbu.thunbu_meeting_system.gotoActivity
import com.thunbu.thunbu_meeting_system.utils.AppUtils
import com.thunbu.thunbu_meeting_system.utils.TreeViewUtils
import com.thunbu.thunbu_meeting_system.view.treeView.NodeModel
import com.thunbu.thunbu_meeting_system.view.treeView.TreeNode
import com.thunbu.thunbu_meeting_system.view.treeView.TreeViewPopupView
import com.thunbu.thunbu_meeting_system.viewMdel.MyAssetsVM


/**
 * @Description: 我的资产
 * @author: Yang.Yang33
 * @Date: 2023/3/22 21:04
 */
class MyAssetsActivity : BaseMVVMActivity<ActivityMyAssetsBinding, MyAssetsVM>() {
    companion object{
        const val TYPE_NAME = "TYPE_NAME"
    }
    lateinit var toolBar: CommonToolBar
    private lateinit var mAdapter: BaseVBQuickAdapter<AssetModel, ItemInventoryListItemOtherBinding>
    val ORIGINAL_PAGE = 1
    var mPage = ORIGINAL_PAGE
    var typeNodes: MutableList<TreeNode<*>> = mutableListOf()
//    var currentCode: String? = null
    var typeName:String? = null

    override fun onMVVMCreated() {
        intent.extras?.getString(TYPE_NAME)?.run {
            bindViews.tvType.text = this
        }
        initToolBar()
        initRv()
        initView()
        initVM()
        loadData()
        TreeViewUtils.loadDataNeedAll(1, typeNodes)
    }

    private fun initVM() {
        vm.assetPage.observe(this){
            bindViews.refereshLayout.finishLoadMore()
            if (mPage == ORIGINAL_PAGE) {
                mAdapter.setNewData(it.list)
            } else {
                mAdapter.addData(it.list?: mutableListOf())
            }
            if(it.total > mAdapter.itemCount+1){
                mPage++
            }else{
                bindViews.refereshLayout.setEnableLoadMore(false)
            }
        }
    }

    private fun initView() {
        bindViews.refereshLayout.setEnableRefresh(false)
        bindViews.refereshLayout.setEnableLoadMoreWhenContentNotFull(false)
        bindViews.refereshLayout.setEnableLoadMore(true)
        bindViews.refereshLayout.setOnLoadMoreListener {
            loadData()
        }
        bindViews.searchBar.ivSearch.setOnClickListener {
            reset()
            loadData()
        }
        bindViews.llCheckType.setOnClickListener {
            showTreeViewPop()
        }

    }

    private fun initRv() {
        bindViews.rv.layoutManager = LinearLayoutManager(this)
        mAdapter = bindViews.rv.bindAdapter { item, position ->
            itemInclude.tvName.text = item.zichanmc
            itemInclude.tvNumber.text = item.zichanbh
            itemInclude.tvType.text = item.zichanflmc
            itemInclude.tvAdr.text = item.cunfangddid
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            gotoActivity<InventoryDetailActivity>{
                putExtra(InventoryDetailActivity.ID,mAdapter.data[position].id)
                putExtra(InventoryDetailActivity.FROM_ASSETS,true)
            }
        }

    }

    private fun initToolBar() {
        toolBar = setToolBar(CommonToolBar::class.java)
        toolBar.setBackAction { finish() }
        toolBar.setTitle("我的资产")
    }

    private fun reset(){
        mPage = ORIGINAL_PAGE
        bindViews.refereshLayout.setEnableLoadMore(true)
    }

    private fun loadData() {
        vm.queryAssetList(
                bindViews.tvType.text.toString(),
                bindViews.searchBar.etSearch.text.toString(),
                mPage,
                20
        )
    }

    private fun showTreeViewPop() {
        if(typeNodes.isNullOrEmpty()){
            ToastUtils.show("接口数据为空,请返回首页重新获取")
            return
        }
        val treeViewPopupView = TreeViewPopupView(this)
        treeViewPopupView.setData(typeNodes) {
            treeViewPopupView.dismiss()
            val nodeModel: NodeModel = it.content as NodeModel
            if (bindViews.tvType.text.toString() == nodeModel.name) return@setData
            bindViews.tvType.text = nodeModel.name
//            currentCode = nodeModel.code
            reset()
            loadData()
        }
        XPopup.Builder(this)
                .customAnimator(EmptyAnimator(bindViews.tvType))
                .asCustom(treeViewPopupView)
                .show()
    }
}